package com.logisticsinquiry.android.ui.trace

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.logisticsinquiry.android.R
import com.logisticsinquiry.android.TraceActivity
import com.logisticsinquiry.android.logic.model.ExpressQueryResponse
import com.logisticsinquiry.android.logic.model.ShipperCodeResponse
import com.logisticsinquiry.android.ui.query.ShipperAdapter
import java.text.ParsePosition
import java.text.SimpleDateFormat

class TraceAdapter(private val fragment: Fragment, private val traceList: List<ExpressQueryResponse.Trace>) :
    RecyclerView.Adapter<TraceAdapter.ViewHolder>() {
    inner class ViewHolder(view: View): RecyclerView.ViewHolder(view){
        val traceDate: TextView = view.findViewById(R.id.trace_date)
        val traceTime: TextView = view.findViewById(R.id.trace_time)
        val traceInfo: TextView = view.findViewById(R.id.trace_info)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.trace_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val trace = traceList[position]
        val dateTime = SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(trace.AcceptTime, ParsePosition(0)).time
        holder.traceDate.text = SimpleDateFormat("yyyy-MM-dd").format(dateTime)
        holder.traceTime.text = SimpleDateFormat("HH:mm:ss").format(dateTime)
        holder.traceInfo.text = trace.AcceptStation
    }

    override fun getItemCount() = traceList.size
}